package com.glc.hms.system.controller.web;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glc.hms.common.controller.BaseController;
import com.glc.hms.common.data.ResponseData;
import com.glc.hms.common.data.SuccessResponseData;
import com.glc.hms.common.page.PageFactory;
import com.glc.hms.common.page.PageVo;
import com.glc.hms.core.annotation.SysLogAnno;
import com.glc.hms.system.entity.SysUser;
import com.glc.hms.system.model.param.UpdatePasswordParam;
import com.glc.hms.system.model.wrapper.UserWrapper;
import com.glc.hms.system.service.ISysRoleService;
import com.glc.hms.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Santa
 * @since 2019-11-26
 */
@Slf4j
@Api(value = "用户", description = "用户接口", basePath = "/webApi/user")
@RestController
@RequestMapping("/webApi/user")
public class UserController extends BaseController {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 获取用户列表
     */
    @ApiOperation(value = "获取用户列表", response = ResponseData.class)
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData getUser(@ApiParam(name = "searchType", value = "条件类型", required = false) String searchType,
                                @ApiParam(name = "keyWords", value = "关键词", required = false) String keyWords) {
        Page<Map<String, Object>> listPage = userService.queryUserByPage(searchType,keyWords);
        Page wrap = new UserWrapper(listPage).wrap();
        PageVo pageInfo = PageFactory.createPageInfo(wrap);
        return new SuccessResponseData(pageInfo);
    }

    /**
     * 修改用户密码
     */
    @ApiOperation(value = "修改用户密码", response = ResponseData.class)
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @SysLogAnno(value = "修改用户密码")
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData updatePassword(@RequestBody UpdatePasswordParam passwordParam) {
        userService.updatePassword(passwordParam);
        return SUCCESS_TIP;
    }

    /**
     * 修改用户
     */
    @ApiOperation(value = "修改用户", response = ResponseData.class)
    @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
    @SysLogAnno(value = "修改用户资料")
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData changeStatus(@RequestBody SysUser sysUser) {
        userService.saveOrUpdate(sysUser);
        sysRoleService.updateUserRole(sysUser.getUserId(),sysUser.getRoleId());
        return SUCCESS_TIP;
    }

    /**
     * 删除用户
     */
    @ApiOperation(value = "删除用户", response = ResponseData.class)
    @RequestMapping(value = "/delUser", method = RequestMethod.GET)
    @SysLogAnno(value = "删除用户")
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData delUser(@ApiParam(name = "userId", value = "用户id", required = true) Long userId) {
        userService.removeById(userId);
        return SUCCESS_TIP;
    }

    /**
     * 添加用户
     */
    @ApiOperation(value = "添加用户", response = ResponseData.class)
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @SysLogAnno(value = "添加用户")
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData addUser(@RequestBody SysUser sysUser) {
        userService.addUser(sysUser);
        return SUCCESS_TIP;
    }

    /**
     * 在线用户列表
     */
    @ApiOperation(value = "在线用户列表", response = ResponseData.class)
    @RequestMapping(value = "/onlineUser", method = RequestMethod.GET)
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData onlineUser() {
        Map<String, Object> onLineUserList = userService.getOnLineUserList();
        return new SuccessResponseData(onLineUserList);
    }

    /**
     * 踢人
     */
    @ApiOperation(value = "踢出用户", response = ResponseData.class)
    @RequestMapping(value = "/kickoutUser", method = RequestMethod.GET)
    @SysLogAnno(value = "踢出用户")
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData kickoutUser(@ApiParam(name = "sessionId", value = "sessionId", required = true) String sessionId) {
        userService.kickoutOnlineUser(sessionId);
        return SUCCESS_TIP;
    }

    /**
     * 踢人
     */
    @ApiOperation(value = "灭霸模式", response = ResponseData.class)
    @RequestMapping(value = "/kickoutOnlineUserMieba", method = RequestMethod.GET)
    @SysLogAnno(value = "灭霸模式")
    //@Permission({"admin"})//管理员才能作此操作
    public ResponseData kickoutOnlineUserMieba() {
        userService.kickoutOnlineUserMieba();
        return SUCCESS_TIP;
    }



}
